﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apache.NMS;
using Apache.NMS.Util;
using Apache.NMS.ActiveMQ;
using Apache.NMS.ActiveMQ.Commands;


namespace ConsoleApplication1
{
    class Program
    {
        private IConnection _connection;
        private ISession _session;

        public const String QUEUE_ADVISORY_DESTINATION = "ActiveMQ.Advisory.Queue";
        public const String TOPIC_ADVISORY_DESTINATION = "ActiveMQ.Advisory.Topic";
        public const String TEMPQUEUE_ADVISORY_DESTINATION = "ActiveMQ.Advisory.TempQueue";
        public const String TEMPTOPIC_ADVISORY_DESTINATION = "ActiveMQ.Advisory.TempTopic";

        public const String ALLDEST_ADVISORY_DESTINATION = QUEUE_ADVISORY_DESTINATION + "," +
                                                           TOPIC_ADVISORY_DESTINATION + "," +
                                                           TEMPQUEUE_ADVISORY_DESTINATION + "," +
                                                           TEMPTOPIC_ADVISORY_DESTINATION;

        Program()
        {
            IConnectionFactory _connfactory = new ConnectionFactory();

            _connection = _connfactory.CreateConnection();
            _connection.Start();
            _session = _connection.CreateSession();


            IQueue _hbQueue = _session.GetQueue("parameters");
            Console.WriteLine(_hbQueue.ToString());
        }
        void GrabQueue()
        {
            Console.Write("Queue");
            IQueueBrowser qb;
            //IQueue q = qb.

//            Console.WriteLine(_qb.Queue.QueueName.ToString());


            IDestination dest = _session.GetTopic(QUEUE_ADVISORY_DESTINATION);
            using (IMessageConsumer consumer = _session.CreateConsumer(dest))
            {
                IMessage message;
                while((message = consumer.ReceiveNoWait()) != null){
                    Console.WriteLine(message.NMSMessageId.ToString());
                    foreach (string val in message.Properties.Values)
                    {
                        Console.Write("Value => " + val);
                    }
                }
                
            }
            
        }
        void EnumerateQueues()
        {
            Console.WriteLine("Listing all Queues on Broker:");

            IDestination dest = _session.GetTopic(QUEUE_ADVISORY_DESTINATION);
            
            using (IMessageConsumer consumer = _session.CreateConsumer(dest))
            {
                IMessage advisory;

                while ((advisory = consumer.Receive(TimeSpan.FromMilliseconds(2000))) != null)
                {
                    ActiveMQMessage amqMsg = advisory as ActiveMQMessage;
                    Console.WriteLine(advisory.NMSTimestamp.ToString());
                    foreach (string value in advisory.Properties.Values)
                    {
                        Console.WriteLine("Value: "+value);
                    }
                    if (amqMsg.DataStructure != null)
                    {
                        DestinationInfo info = amqMsg.DataStructure as DestinationInfo;
                        if (info != null)
                        {
                            Console.WriteLine("->Queue: " + info.Destination.ToString());
                        }
                    }
                }
            }
            Console.WriteLine("Listing Complete.");
        }

        void EnumerateTopics()
        {
            Console.WriteLine("Listing all Topics on Broker:");

            IDestination dest = _session.GetTopic(TOPIC_ADVISORY_DESTINATION);

            using(IMessageConsumer consumer = _session.CreateConsumer(dest))
            {
                
                IMessage advisory;

                while((advisory = consumer.Receive(TimeSpan.FromMilliseconds(2000))) != null)
                {
                    ActiveMQMessage amqMsg = advisory as ActiveMQMessage;
                    
                    
                    if(amqMsg.DataStructure != null)
                    {
                        DestinationInfo info = amqMsg.DataStructure as DestinationInfo;
                        if(info != null)
                        {

                            Console.WriteLine("   Topic: " + info.Destination.ToString() );
                        }
                    }
                }
            }
            Console.WriteLine("Listing Complete.");
        }

        void EnumerateDestinations()
        {
            Console.WriteLine("Listing all Destinations on Broker:");

            IDestination dest = _session.GetTopic(ALLDEST_ADVISORY_DESTINATION);

            using (IMessageConsumer consumer = _session.CreateConsumer(dest))
            {
                IMessage advisory;

                while((advisory = consumer.Receive(TimeSpan.FromMilliseconds(2000))) != null)
                {
                    ActiveMQMessage amqMsg = advisory as ActiveMQMessage;

                    if(amqMsg.DataStructure != null)
                    {
                        DestinationInfo info = amqMsg.DataStructure as DestinationInfo;
                        if(info != null)
                        {
                            string destType = info.Destination.IsTopic ? "Topic" : "Queue";
                            destType = info.Destination.IsTemporary ? "Temporary" + destType : destType;
                            Console.WriteLine("   " + destType + ": " + info.Destination.ToString() );
                        }
                    }
                }
            }
            Console.WriteLine("Listing Complete.");
        }

        static void Main(string[] args)
        {
            Program prog = new Program();
            prog.EnumerateQueues();
            prog.EnumerateDestinations();
            prog.EnumerateTopics();
            prog.GrabQueue();

            Console.ReadKey(true);
        }
    }
}
